package com.project.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.project.demo.entity.Speaker;
import com.project.demo.service.SpeakerService;
import com.project.demo.service.CosService;
import com.project.demo.controller.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;


/**
 * 报告嘉宾：(Speaker)表控制层
 *
 */
@Slf4j
@RestController
@RequestMapping("/speaker")
public class SpeakerController extends BaseController<Speaker, SpeakerService> {

    /**
     * 报告嘉宾对象
     */
    @Autowired
    public SpeakerController(SpeakerService service) {
        setService(service);
    }
    
    @Autowired
    private CosService cosService;

    @PostMapping("/add")
    @Transactional
    public Map<String, Object> add(HttpServletRequest request) throws IOException {
        Map<String,Object> paramMap = service.readBody(request.getReader());
        this.addMap(paramMap);
        return success(1);
    }
    
    /**
     * 报告嘉宾上传接口
     */
    @PostMapping("/upload")
    public Map<String, Object> upload(@RequestParam("file") MultipartFile file) {
        log.info("报告嘉宾上传接口被调用");
        if (file.isEmpty()) {
            return error(30000, "没有选择文件");
        }
        try {
            // 使用腾讯云COS上传
            String fileUrl = cosService.uploadFile(file);
            
            log.info("报告嘉宾文件上传成功，URL: {}", fileUrl);
            
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("url", fileUrl);
            return success(jsonObject);
        } catch (Exception e) {
            log.error("报告嘉宾COS上传失败：{}", e.getMessage(), e);
            // 如果COS上传失败，尝试本地存储作为备用方案
            try {
                String userDir = System.getProperty("user.dir");
                String filePath;
                if (userDir.endsWith("server")) {
                    filePath = userDir + "\\target\\classes\\static\\upload\\";
                } else {
                    filePath = userDir + "\\server\\target\\classes\\static\\upload\\";
                }
                java.io.File targetDir = new java.io.File(filePath);
                if (!targetDir.exists() && !targetDir.isDirectory()) {
                    targetDir.mkdirs();
                }
                String fileName = file.getOriginalFilename();
                java.io.File dest = new java.io.File(filePath + fileName);
                file.transferTo(dest);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("url", "/api/upload/" + fileName);
                log.warn("报告嘉宾COS上传失败，使用本地存储备用方案");
                return success(jsonObject);
            } catch (java.io.IOException ioException) {
                log.error("报告嘉宾本地存储也失败：{}", ioException.getMessage(), ioException);
            }
        }
        return error(30000, "上传失败");
    }

}


